<template>
<el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" width="50%" top="60px">
    <el-form style="height:500px;overflow:auto;padding-right:20px" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '170px' : '150px'">
        <el-row v-if="activeName=='first'">
            <el-col :span="12">
                <el-form-item label="商名名称" prop="name">
                    <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="单价" prop="price">
                    <el-input  v-model="dataForm.price" type="number"></el-input>
                </el-form-item>
            </el-col>
             <el-col :span="12">
                <el-form-item label="库存" prop="price">
                    <el-input  v-model="dataForm.num" type="number"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="商户名称" prop="merchantId">
                    <el-select v-model="dataForm.merchantId" placeholder="所属商户" clearable style="width:100%">
                        <el-option v-for="item in shList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
                <el-form-item label="标签" prop="type">
                    <ren-select v-model="dataForm.typeArr" multiple dict-type="FishermanType" placeholder="请选择标签" style="width:100%"></ren-select>
                </el-form-item>
            </el-col> -->
            <el-col :span="24">
                <el-form-item label="商品详情" prop="productsDetail">
                    <el-input v-model="dataForm.productsDetail" type="textarea" rows="4"></el-input>
                </el-form-item>
            </el-col>
             <el-col :span="24">
                <el-form-item label="封面(最多1张)" prop="coverImage">
                    <el-upload class="avatar-uploader" :action="url" :show-file-list="false" :on-success="(res, file, fileList)=>successHandle1(res, file, fileList,1)" :before-upload="beforeUploadHandle">
                        <img v-if="dataForm.coverImg" :src="dataForm.coverImg" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>

    <template slot="footer">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
</el-dialog>
</template>

<script>
import Cookies from "js-cookie";
import debounce from "lodash/debounce";
export default {
    data() {
        return {
            url: `${
        window.SITE_CONFIG["apiURL"]
      }/oss/file/aliyunUpload?access_token=${Cookies.get("access_token")}`,
            dialogImageUrl: '',
            dialogVisible: false,
            visible: false,
            activeName: "first",
            fishOptions: [],
             shList: [],
            dataForm: {
                id: "",
                merchantType: 10,
                needNum: '',
                fishermanId: '',
                fishermanName: '',
                name: '',
                notice: "",
                price: '',
                feature: '',
                tips: '',
                suitNum: '',
                information: '',
                caildTime: '',
                pic: "",
               
                type: '',
            },
        };
    },
    computed: {
        dataRule() {
            return {
                name: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                price: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                notice: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                fishermanId: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
            };
        },
    },
    components: {
    },
    watch: {
         "activeName"(newval, oldval) {
            if (newval == 'second') {
                this.$nextTick(() => {
                    this.$refs.infoIntroduceRef.init();
                })
            }
        },
    },
    created() {},
    methods: {
          getEditor(val) {
            this.dataForm.information = val;
        },
        //预览
        getsh() {
            this.$http.get('/scenic/merchant/info/getMerchantDownBox').then(res=>{
                console.log(res)
                if(res.data.code==0){
                     this.shList=res.data.data;
                }
            })
        },
        // 上传之前
        beforeUploadHandle(file) {
            if (
                file.type !== "image/jpg" &&
                file.type !== "image/jpeg" &&
                file.type !== "image/png" &&
                file.type !== "image/gif"
            ) {
                this.$message.error(this.$t("upload.tip", {
                    format: "jpg、png、gif"
                }));
                return false;
            }
        },
        // 上传成功
        successHandle1(res, file, fileList, index) {
            if (res.code !== 0) {
                return this.$message.error(res.msg);
            }
            let key = 'fileList' + index;
            this[key].unshift({
                name: "",
                url: res.data.url,
                picType: index,
            });
            if (index == 1) {
                this.dataForm.coverImage = res.data.url;
            }
        },
        init() {
            this.visible = true;
             this.getsh();
            this.$nextTick(() => {
                this.$refs["dataForm"].resetFields();
                if (this.dataForm.id) {
                    this.getInfo();
                }
            });
        },
        // 获取信息
        getInfo() {
            this.$http
                .get("/scenic/merchant/product/" + this.dataForm.id)
                .then(({
                    data: res
                }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    this.dataForm = {
                        ...this.dataForm,
                        ...res.data,
                    };
                })
                .catch(() => {});
        },
        // 表单提交
        dataFormSubmitHandle: debounce(
            function () {
                this.$refs["dataForm"].validate((valid) => {
                    if (!valid) {
                        return false;
                    }
                    if (this.dataForm.fishermanId) {
                        this.dataForm.fishermanName = this.fishOptions.filter((item) => item.id == this.dataForm.fishermanId)[0].name;
                    }
                    this.$http[!this.dataForm.id ? "post" : "put"](
                            "/scenic/merchant/product",
                            this.dataForm
                        )
                        .then(({
                            data: res
                        }) => {
                            if (res.code !== 0) {
                                return this.$message.error(res.msg);
                            }
                            this.$message({
                                message: this.$t("prompt.success"),
                                type: "success",
                                duration: 500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit("refreshDataList");
                                },
                            });
                        })
                        .catch(() => {});
                });
            },
            1000, {
                leading: true,
                trailing: false
            }
        ),
    },
};
</script>

<style lang="scss" scoped>
h3 {
    padding: 30px 0 15px;
    width: 94%;
    margin: 0 auto 30px;
    border-bottom: 1px solid #ddd;
    color: #303133;
    font-size: 22px;
    overflow: hidden;
}

.aui-content .ql-container {
    height: auto
}

.ql-container.ql-snow {
    height: 500px;
}

.rooms .item {
    display: inline-block;
    margin-right: 15px;
    position: relative;

    .el-icon-error {
        position: absolute;
        right: 6px;
        top: -6px;
        font-size: 20px;
        color: #dd0606;
    }
}
</style>
